<template>
  <div class="register-container">
    <div class="image-container">
      <img :src="Img" class="background-image" />
    </div>
    <div class="form-wrapper">
      <div class="form-header">
        <h1>Register</h1>
        <p>Please provide personal information</p>
      </div>
      <a-form
        :model="formState"
        name="register"
        class="auth-form"
        @finish="onFinish"
        @finishFailed="onFinishFailed"
      >
        <a-form-item
          class="form-item"
          label="Email"
          name="email"
          :rules="[{ required: true, message: 'Please input your email!' }]"
        >
          <a-input v-model:value="formState.email">
            <template #prefix>
              <MailOutlined class="site-form-item-icon" />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item
          class="form-item"
          label="Username"
          name="userName"
          :rules="[{ required: true, message: 'Please input your username!' }]"
        >
          <a-input v-model:value="formState.userName">
            <template #prefix>
              <UserOutlined class="site-form-item-icon" />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item
          class="form-item"
          label="Password"
          name="password"
          :rules="[{ required: true, message: 'Please input your password!' }]"
        >
          <a-input-password v-model:value="formState.password">
            <template #prefix>
              <LockOutlined class="site-form-item-icon" />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item>
          <a-button type="primary" html-type="submit" class="submit-btn" :loading="loading">
            Register
          </a-button>
        </a-form-item>
      </a-form>
      <div class="additional-links">
        <a href="/login">← Back to Login</a>
      </div>
    </div>
  </div>
</template>

<script setup>
// 在这里引入你的自定义图片
import Img from '@/assets/img.png' // 你需要替换成你自己的图片路径
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import useAuthStore from '@/stores/auth.js'
import { register } from '@/api/register'
import { message } from 'ant-design-vue'
import { ref } from 'vue'
import { MailOutlined, UserOutlined, LockOutlined } from '@ant-design/icons-vue'

const router = useRouter()
const authStore = useAuthStore()

const formState = reactive({
  email: '',
  userName: '',
  password: ''
})
const loading = ref(false)
const onFinish = async (values) => {
  loading.value = true
  try {
    const response = await register(formState.email, formState.userName, formState.password);
    if(response.data.code === 200) {
      message.success('Register successfully!');
      router.push('/login'); // 跳转到登录页面
    }else{
      message.error(response.data.message);
      return;
    }
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}

const onFinishFailed = (errorInfo) => {
  console.log('Failed:', errorInfo)
}
</script>

<style scoped>
.register-container {
  display: flex;
  min-height: 100vh;
  background: #ffffff;
}

.image-container {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.background-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0.9;
  filter: grayscale(10%);
}

.form-wrapper {
  width: 100%;
  max-width: 480px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h1 {
  font-size: 28px;
  color: #2d3748;
  margin-bottom: 8px;
  font-weight: 600;
}

.form-header p {
  color: #718096;
  font-size: 14px;
}

.auth-form {
  :deep(.ant-input-affix-wrapper) {
    border: 1px solid #d9d9d9 !important;
    border-radius: 6px;
    padding: 8px 11px;
    transition: all 0.3s;

    &:hover {
      border-color: #4096ff !important;
    }

    &:focus-within {
      border-color: #4096ff !important;
      box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2) !important;
    }
  }

  :deep(.ant-input-password) {
    border: 1px solid #d9d9d9 !important;
    border-radius: 6px;
    padding: 8px 11px;

    .ant-input {
      border: none !important;
      box-shadow: none !important;
    }
  }
}

.submit-btn {
  width: 100%;
  height: 48px;
  font-weight: 600;
  border-radius: 8px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  transition: all 0.3s;
  border: none;

  &:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(118, 75, 162, 0.3);
  }
}

.additional-links {
  text-align: center;
  margin-top: 24px;

  a {
    color: #667eea;
    text-decoration: none;
    font-weight: 500;
    transition: color 0.3s;

    &:hover {
      color: #764ba2;
    }
  }
}

@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
  }

  .image-container {
    height: 200px;
  }

  .form-wrapper {
    max-width: 100%;
    margin-top: -60px;
    border-radius: 24px 24px 0 0;
  }
}
</style>
